package com.linkage.bookshop.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.linkage.bookshop.authentication.CrendentialSessionService;
import com.linkage.bookshop.model.Role;
import com.linkage.bookshop.model.User;
import com.linkage.bookshop.service.inter.UserService;


@Controller
@RequestMapping("/user/*") 
public class UserController {
	
	@Resource
	private UserService userService;
	@Resource
	private CrendentialSessionService crendentialSessionService;
	
	@RequestMapping(value = "/getLoginInfo", produces = "application/json", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getLoginInfo(HttpServletRequest req){
		Map<String, Object> params = new HashMap<>();
		String username = crendentialSessionService.getUsername(req);
		if(username == null){
			params.put("loginStatus",false);
			return params;
		}
		String nickname = userService.loadNickname(username);
		Double wealth = userService.loadWealth(username);
		params.put("loginStatus", true);
		params.put("username", username);
	    params.put("nickname", nickname);
	    params.put("wealth", wealth);
		return params;
	}
	
	@RequestMapping(value = "/checkUsername/{username}/", produces = "application/json", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> usernameCheck(@PathVariable("username") String username){
		Map<String, Object> params = new HashMap<>();
		boolean result = userService.checkUsername(username);
	    params.put("result", result);
		return params;
	}
	
	
	
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public String register(@RequestParam("username") String username, @RequestParam("password") String password){
		if(userService.addUser(username, password)){
			return "home";
		}
		
		return "error";
	}

	@RequestMapping(value = "/selfHome/{nickname:.*}", method = RequestMethod.GET)
	public String selfHome(@PathVariable("nickname") String nickname){	
		return "self_home";
	}
	
	@RequestMapping(value = "/modifyNickname/{nickname}", produces = "application/json", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modifyNickname (@PathVariable("nickname") String nickname, HttpServletRequest req){
		Map<String, Object> params = new HashMap<>();
		String username = crendentialSessionService.getUsername(req);
		boolean result = userService.modifyNickname(username, nickname);
		params.put("result",result);
		return params;
	}
	
	@RequestMapping(value = "/modifyPassword", produces = "application/json", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modifyPassword (@RequestParam("oldPassword") String oldPassword, @RequestParam("password") String password,HttpServletRequest req ){
		Map<String, Object> params = new HashMap<>();
		String username = crendentialSessionService.getUsername(req);
		boolean result = userService.checkPassword(username, oldPassword);
		if(!result){
			params.put("result", false);
			return params;
		}
		result = userService.modifyPassword(username, password);
		if(!result){
			params.put("result", false);
			return params;
		}
		params.put("result", true);
		return params;
	}
	
	@RequestMapping(value = "/findMember/{username}/", produces = "application/json", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> findMember (@PathVariable("username") String username ){
		Map<String, Object> params = new HashMap<>();
		User member = userService.loadMember(username);
		if(member != null){
			List<User> members = new ArrayList<>();
			members.add(member);
			params.put("members", members);
		}
		return params;
	}	
	
	@RequestMapping(value = "/listMembers/{pageNum:[1-9][0-9]*}", produces = "application/json", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> listMembers (@PathVariable("pageNum") int pageNum ){
		Map<String, Object> params = new HashMap<>();
		List<User> members = userService.listMembers(15, pageNum);
		params.put("members", members);
		return params;
	}
	
	@RequestMapping(value = "/lockMember/{id:[1-9][0-9]*}", produces = "application/json", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> lockMember (@PathVariable("id") int id ){
		Map<String, Object> params = new HashMap<>();
		boolean result = userService.lockMember(id);
		params.put("result", result);
		return params;
	}
	
	@RequestMapping(value = "/unlockMember/{id:[1-9][0-9]*}", produces = "application/json", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> unlockMember (@PathVariable("id") int id ){
		Map<String, Object> params = new HashMap<>();
		boolean result = userService.unlockMember(id);
		params.put("result", result);
		return params;
	}
	
	@RequestMapping(value = "/listAdmins/{pageNum:[1-9][0-9]*}", produces = "application/json", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> listAdmins (@PathVariable("pageNum") int pageNum ){
		Map<String, Object> params = new HashMap<>();
		List<User> admins = userService.listAdmins(15, pageNum);
		params.put("admins", admins);
		return params;
	}
	
	@RequestMapping(value = "/lockAdmin/{id:[1-9][0-9]*}", produces = "application/json", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> lockAdmin (@PathVariable("id") int id ){
		Map<String, Object> params = new HashMap<>();
		boolean result = userService.lockAdmin(id);
		params.put("result", result);
		return params;
	}
	
	@RequestMapping(value = "/unlockAdmin/{id:[1-9][0-9]*}", produces = "application/json", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> unlockAdmin (@PathVariable("id") int id ){
		Map<String, Object> params = new HashMap<>();
		boolean result = userService.unlockAdmin(id);
		params.put("result", result);
		return params;
	}
	
	@RequestMapping(value = "/findAdmin/{username}/", produces = "application/json", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> findAdmin (@PathVariable("username") String username ){
		Map<String, Object> params = new HashMap<>();
		User admin = userService.loadAdmin(username);
		if(admin != null){
			List<User> admins = new ArrayList<>();
			admins.add(admin);
			params.put("admins", admins);
		}
		return params;
	}
	
	@RequestMapping(value = "/addAdmin", produces = "application/json", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addAdmin (@RequestParam("admin") String admin ){
		Map<String, Object> params = new HashMap<>();
		JSONObject json = JSONObject.fromObject(admin);
		User adminUser = new User();
		Set<Role> roles = new HashSet<>();
		Role role = null;
		Object[] objects = json.getJSONArray("roles").toArray();
		for(int i=0;i<objects.length;i++){
			role = new Role();
			role.setName(objects[i].toString());
			roles.add(role);
		}
		adminUser.setUsername(json.getString("username"));
		adminUser.setPassword(json.getString("password"));
		adminUser.setNickname(json.getString("nickname"));
		adminUser.setRoles(roles);
		boolean result = userService.addAdmin(adminUser);
		params.put("result", result);
		return params;
	}
	
	@RequestMapping(value = "/manageAdmin", produces = "application/json", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> manageAdmin (){
		Map<String, Object> params = new HashMap<>();
		params.put("result", true);
		return params;
	}
	
	@RequestMapping(value = "/manageMember", produces = "application/json", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> manageMember (){
		Map<String, Object> params = new HashMap<>();
		params.put("result", true);
		return params;
	}	
	
	
}